package uutalk.netcloud.talk.base.util;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import uutalk.netcloud.talk.model.bean.FileBean;

/**
 * Created by hujie on 16/6/23.
 */
public class FileUtil {

    public static boolean isSdCardExist() {
        return Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在
    }


    /**
     * 获取存储路径,没有SD卡 就存储到本地路径下面
     * @param context
     * @return
     */
    private static String getRootCachePath(Context context) {

        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在
        if (sdCardExist) {
            return Environment.getExternalStorageDirectory().getPath();//获取跟目录
        } else {
            return context.getCacheDir().getPath();
        }
    }

    /**
     * 删除文件
     * @param path
     * @return
     */
    public static boolean deleteFile(String path) {
        if (TextUtils.isEmpty(path)) {
            return true;
        }

        File file = new File(path);
        if (file.exists()) {
            if (file.isFile()) {
                return file.delete();
            } else if (file.isDirectory()) {
                for (File f : file.listFiles()) {
                    if (f.isFile()) {
                        f.delete();
                    } else if (f.isDirectory()) {
                        deleteFile(f.getAbsolutePath());
                    }
                }
                return file.delete();
            }
            return false;
        }
        return true;
    }


    public static String getCachePath(Context context, String path) {
        return getRootCachePath(context) + path;
    }

    public static String getExternalFile(String subFolder, String fileName) {
        try {
            String subFolderPath = getSDCardPath() + "/uutalk/"
                    + subFolder + "/";
            File fi = new File(subFolderPath);
            if (!fi.exists()) {
                fi.mkdirs();
            }
            return fi.getAbsolutePath() + "/" + fileName;
        } catch (Exception exp) {
            exp.printStackTrace();
            return null;
        }

    }
    public static String getSDCardPath() {
        String mSDCARD = Environment.getExternalStorageDirectory().toString();
        if (mSDCARD == null) {
            mSDCARD = "/sdcard";
        }
        return mSDCARD;
    }

    public static void writeObject2File(String filePath, Object obj){
        final File file =new File(filePath);
        //序列化
        try {
            FileOutputStream fis=new FileOutputStream(file);
            ObjectOutputStream ois=new ObjectOutputStream(fis);
            ois.writeObject(obj);
            ois.close();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取指定目录内所有文件路径
     * @param dirPath 需要查询的文件目录
     * @param _type 查询类型，比如mp3什么的
     */
    public static ArrayList<File> getAllFiles(String dirPath, String _type) {
        File f = new File(dirPath);
        if (!f.exists()) {//判断路径是否存在
            return null;
        }

        File[] files = f.listFiles();

        if(files==null){//判断权限
            return null;
        } else {
            ArrayList<File> arrayList = new ArrayList<File>();
            for (File _file : files) {//遍历目录
                if(_file.isFile() && _file.getName().endsWith(_type)){
                    arrayList.add(_file);
                }
            }
            return arrayList;
        }
    }

    /**
     * 遍历找出所有指定类型的文件
     * @param type
     * @return
     */
    public static List<FileBean> listFiles(String type){
        List<FileBean> fileList = new ArrayList<FileBean>();
        File sdRoot = new File(Environment.getExternalStorageDirectory() + "/");
        Collection<File> collection = FileUtils.listFiles(sdRoot, new String[]{type}, true);
        int count = 0;
        FileBean bean = null;
        for (File item : collection) {
            Log.e("Name: ",item.getName()+"");
            Log.e("Path: ", item.getAbsolutePath() + "");
            count ++;
            bean = new FileBean();
            bean.setFilename(item.getName());
            bean.setFilePath(item.getAbsolutePath());
            bean.setSelected(false);
            fileList.add(bean);
        }
        Log.e("count", "count>>"+count);
        return fileList;
    }
}
